package com.zbs.framework.start.controller;

import com.zbs.framework.security.utils.ThreadLocalClient;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/test")
/*
  参数详解：
    value： Controller显示名称 如果不填，会以Controller类名进行显示
    group： API分组，默认分组名称： 默认分组，多个Controller可以以一个名称进行同组
 */
@ApiTags(value = "测试控制器",group = "默认分组")
public class TestController3 {

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping(value = "/v1/getAllUrl.do", method = RequestMethod.POST)
    public String getAllUrl() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

//		List<String> urlList = new ArrayList<>();
//		for (RequestMappingInfo info : map.keySet()) {
//			// 获取url的Set集合，一个方法可能对应多个url
//			Set<String> patterns = info.getPatternsCondition().getPatterns();
//
//			for (String url : patterns) {
//				urlList.add(url);
//			}
//		}
        map.forEach((k,v) -> {
            System.out.println(k.getPathPatternsCondition().getPatterns());
            System.out.println(v);
        });

//        List<Map<String, String>> list = new ArrayList<>();
//        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
//            Map<String, String> map1 = new HashMap<>();
//            RequestMappingInfo info = m.getKey();
//            HandlerMethod method = m.getValue();
//            PatternsRequestCondition p = info.getPatternsCondition();
//            for (String url : p.getPatterns()) {
//                map1.put("url", url);
//            }
//            map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
//            map1.put("method", method.getMethod().getName()); // 方法名
//            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
//            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
//                map1.put("type", requestMethod.toString());
//            }
//
//            list.add(map1);
//        }
//        return JSONObject.toJSONString(list);
        return "";

    }


    @ApiOperation(value = "测试")
    @PostMapping("/test.do")
    public String test(HttpServletResponse response,HttpServletRequest request){
        return "测试";
    }


    @ApiOperation(value = "测试3")
    @PostMapping("/test3")
    @PreAuthorize("hasAuthority('test::test')")
    public String test3(HttpServletResponse response,HttpServletRequest request){
        System.out.println(ThreadLocalClient.getUserName());
        return ThreadLocalClient.getUserName();
    }

}
